import CustomPage from './base/CustomPage'

CustomPage({  
  onShareAppMessage() {
    return {
      title: '尚小镜',
      path: 'page/home/index'
    }
  },
  data: {
    doctorCurrIndex: 0,  // 当前显示的图片索引
    swiperMargin: '0rpx',   // 两侧留白
    bannerKeyword: 'Index',
    searchKeyword: '',
    phone: '15801466758',
    showCallModal: false,
    topRecommendBanner: {
      title: '',
      imageUrl: '',
      link: '',
    },
    bottomLinksBanner: {
      title: '',
      imageUrl: '',
      link: '',
    },
    phoneNumbers: [
      { label: '刀客', number: '021-12345678' },
      { label: '旭东', number: '021-87654321' },
      { label: '齐亮', number: '021-11112222' }
    ],
    latitude: 30.59941511696292,  // 替换为实际纬度
    longitude: 114.2453670501709,  // 替换为实际经度
    markers: [{
      id: 1,
      latitude: 30.59941511696292,  // 替换为实际纬度
      longitude: 114.2453670501709,  // 替换为实际经度
      name: '武汉尚小镜内镜诊疗中心',
      callout: {
        content: '武汉尚小镜内镜诊疗中心',
        padding: 10,
        borderRadius: 5,
        display: 'ALWAYS'
      }
    }],
    address: '湖北省武汉市江汉区泛海国际SOHO城8栋2楼',  // 替换为实际地址
    currentIndex: 0,
    currentTab: 'intro',
    doctors: [
      {
        id: '',
        avatar: '',
        linkUrl: ''
      }
    ],

    gridList: [
      { icon: 'cloud://cloud1-9gel6ndh8e555155.636c-cloud1-9gel6ndh8e555155-1345681178/icon_intro.png', name: '预约' },
      { icon: 'cloud://cloud1-9gel6ndh8e555155.636c-cloud1-9gel6ndh8e555155-1345681178/icon_intro.png', name: '挂号' },
      { icon: 'cloud://cloud1-9gel6ndh8e555155.636c-cloud1-9gel6ndh8e555155-1345681178/icon_intro.png', name: '咨询' },
      { icon: 'cloud://cloud1-9gel6ndh8e555155.636c-cloud1-9gel6ndh8e555155-1345681178/icon_intro.png', name: '回访' },
      { icon: 'cloud://cloud1-9gel6ndh8e555155.636c-cloud1-9gel6ndh8e555155-1345681178/icon_intro.png', name: '推荐' },
    ],

    banners: [
      { id: '', imageUrl: '', linkUrl: '' },
    ],
    //默认高亮
    activeIndex: 0,
    itemLefts: [],
    itemScales: [],
    touchStartX: 0,

  },

  // 监听 swiper 切换事件
  onSwiperChange(e) {
    this.setData({
      doctorCurrIndex: e.detail.current,
    });
  },

  handleSwiperChange(e) {
    const { current } = e.detail;
    this.setData({
      currentIndex: current
    });
  },

  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      currentTab: tab
    });
  },

  consultDoctor(e) {
    const doctorId = e.currentTarget.dataset.id;
    // 实现在线咨询逻辑
  },

  makeAppointment(e) {
    const doctorId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/appointment/index?doctorId=${doctorId}`
    });
  },

  // 拨打电话事件
  phoneOn() {
    wx.makePhoneCall({
      phoneNumber: this.data.phone,
      success: function() {
        console.log("拨打电话成功！")
      },
      fail: function() {
        console.log("拨打电话失败")
      }
    })
  },

  showCallModal() {
    this.setData({
      showCallModal: true
    });
  },

  hideCallModal() {
    this.setData({
      showCallModal: false
    });
  },

  makePhoneCall(e) {
    const phoneNumber = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber,
      success: () => {
        this.hideCallModal();
      }
    });
  },


  // 打开导航
  openLocation() {
    wx.openLocation({
      latitude: this.data.latitude,
      longitude: this.data.longitude,
      name: '尚小镜 - 总店',
      address: this.data.address,
      scale: 16
    });
  },

  // 点击地图
  handleMapTap() {
    this.openLocation();
  },

  kindToggle(e) {
    const id = e.currentTarget.id
    const list = this.data.list
    for (let i = 0, len = list.length; i < len; ++i) {
      if (list[i].id === id) {
        list[i].open = !list[i].open
      } else {
        list[i].open = false
      }
    }
    // const extendedList = this.data.extendedList.map((item) => ({...item, open: false}))
    this.setData({
      list,
      // extendedList,

    })
  },
  kindExtenedListToggle(e) {
    const id = e.currentTarget.id
    const extendedList = this.data.extendedList
    for (let i = 0, len = extendedList.length; i < len; ++i) {
      if (extendedList[i].id === id) {
        extendedList[i].open = !extendedList[i].open
      } else {
        extendedList[i].open = false
      }
    }
    const list = this.data.list.map((item) => ({...item, open: false}))
    this.setData({
      extendedList,
      list,
    })
  },
  themeToggle() {
    const App = getApp()

    if (App.themeChanged) {
      if (App.globalData.theme === 'light') {
        App.themeChanged('dark')
      } else {
        App.themeChanged('light')
      }
    }
  },
  onSearchInput(e) {
    const keyword = e.detail.value;
    this.setData({
      searchKeyword: keyword
    });
    this.filterServices(keyword);
  },

  onSearch() {
    this.filterServices(this.data.searchKeyword);
  },

  clearSearch() {
    this.setData({
      searchKeyword: '',
      services: this.data.originalServices
    });
  },

  toCompanyDetail() {
    wx.navigateTo({
      url: '/page/company/index',
    })
  },

  toRecommend(event) {
    const linkUrl = event.currentTarget.dataset.param1;
    console.log(linkUrl);
      wx.navigateTo({
        // url: '/page/recommend/index',
        url: linkUrl,
      });
  },

  toMembersprofile() {
    wx.navigateTo({
      url: '/page/membersprofile/index',
    })
  },

  toCredits() {
    /*
    wx.switchTab({
      url: '/page/credits/index',
    });
    */

  
    wx.switchTab({
      url: '/page/points/index',
    });
    

    /*
   wx.navigateTo({
    url: '/page/supershop/index',
  });
  */
  },

  toLinkservice(event) {
    const linkUrl = event.currentTarget.dataset.param1;
    wx.navigateTo({
      // url: '/page/linkservice/index',
      url: linkUrl,
    })
  },

  toTeam(event) {
    const linkUrl = event.currentTarget.dataset.param1;
    const id = event.currentTarget.dataset.param2;
    wx.navigateTo({
      // url: '/page/team/index',
      url: linkUrl + '?param1=' + id,
    })
  },

  toDoctor(event) {
    const linkUrl = event.currentTarget.dataset.param1;
    const id = event.currentTarget.dataset.param2;
    wx.navigateTo({
      // url: '/page/doctor/index',
      url: linkUrl + '?param1=' + id,
    })
  },




  onLoad() {
    const { init } = require('./../../wxCloudClientSDK.umd.js')
   // 指定云开发环境 ID
   wx.cloud.init({
     env: "cloud1-9gel6ndh8e555155", // 当前的云开发环境 ID
   });
   
   const client = init(wx.cloud);

   let that = this;

   client.models.company_info.get({}).then(res => {
    console.log("地址：" + (res.data.area.replace(/,/g, '')) + res.data.address);
    that.data.address = res.data.area.replace(/,/g, '') + res.data.address;
  }).catch(res => {
      console.log(res)
   });
   // 获取首页顶部轮播图
   client.models.swiper_info.list({
     filter: {
       where: {
        $and: [{
          categoryDesc: {
            $eq: 'IndexTop'
          },
        }, {
          enableActive: {
            $eq: 0
          }
        }
      ],
       }
     },
     pageSize: 10,
     pageNumber: 1,
     getCount: true
  }).then(res => {
    console.log("swiper_info: " + res.data.records);
    res.data.records.forEach(item => {
      console.log(item);
  });
    this.parseSwiperData(res.data.records);
  }).catch(res => {
    console.log("swiper_info err: " + res)
  });
// 获取首页医生信息图片
  const { bannerKeyword } = this.data;
    client.models.banner_info.list({
      filter: {
        where: {
          $and: [{
            position: {
              $in: ['IndexTopBanner', 'IndexBottom']
            },
          },
        ],   
        },
      },
    }).then(res => {
      console.log("banner_info: " + res.data.records);
      res.data.records.forEach(item => {
        console.log(item);
      });
      this.parseBannerData(res.data.records);
    }).catch(res => {
      console.log("banner_info err: " + res)
    });

    // 获取首页顶部轮播图
   client.models.swiper_info.list({
        filter: {
          where: {
          $and: [{
            categoryDesc: {
              $eq: 'IndexDoctor'
            },
          }, {
            enableActive: {
              $eq: 0
            }
          }
        ],
          }
        },
        pageSize: 10,
        pageNumber: 1,
        getCount: true
      }).then(res => {
      res.data.records.forEach(item => {
        console.log(item);
      });
      this.parseSwiperDoctorData(res.data.records);
    }).catch(res => {
      console.log("swiper_info err: " + res)
    });
  },

  // 解析首页轮播图
  parseSwiperData(data) {
    const parsedData = data.map(item => {
      return {
        id: item._id,
        imageUrl: item.image,
        linkUrl: item.link,
      };
    });

    this.setData({
      banners: parsedData
    });
  },

  // 解析首页轮播图
  parseSwiperDoctorData(data) {
    const parsedData = data.map(item => {
      return {
        id: item._id,
        avatar: item.image,
        linkUrl: item.link
      };
    });
    this.setData({
      doctors: parsedData
    });

  },

  // 解析首页Banner图
  parseBannerData(data) {
    data.forEach(item => {
      if (item.position == "IndexBottom") {
        this.setData({
          bottomLinksBanner: {
            title: item.title,
            imageUrl: item.imageUrl,
            link: item.link
          }
        });
      } else {
        this.setData({
          topRecommendBanner: {
            title: item.title,
            imageUrl: item.imageUrl,
            link: item.link
          }
        });

      }
    });

    console.log("topRecommendBanner: " + this.data.topRecommendBanner.imageUrl);
    console.log("bottomLinksBanner: " + this.data.bottomLinksBanner);
  }


});



